<?php

//Hämtar in nödvändiga filer
require_once ("RegisterHandler.php");
require_once ("RegisterView.php");


	class RegisterController{
		
		public function doControl(Database $db){
			
			$rh = new RegisterHandler($db);
			$rv = new RegisterView();
			$pw = new PageView();

			$body = $rv->DoMessageBox(RegisterView::INFO);			
			$body .= $rv->DoRegisterBox();
			
			if($rv->TriedToAbort()){
				return false;
			}			
			
			if($rv->TriedToRegister()){
			
				$messageArray = $rv->Validate();
				
				if(!$rh->UserNameCompare($rv->GetUserName())){
					$messageArray[] = RegisterView::USERNAME_TAKEN;	
				}
							
				if($messageArray  == null){
						
					if($rh->SaveUser($rv->GetUserName(), $rv->GetPassword())){
						$body .= $rv->DoMessageBox(RegisterView::REGISTER);						
					}
					else{
						$body .= $rv->DoMessageBox(RegisterView::REGISTER_FAILED);						
					}
				}
				else{
					$body .= $rv->DoMessageBox(RegisterView::REGISTER_FAILED);
					$body .= $rv->DoMessageBox($messageArray);						
				}
			}
			
			return "<form method='post'>$body</form>";
		}
		
	}
